<?php
namespace app\api\controller;
use app\common\controller\BaseApi;

/*网易云Im  api*/
class YunxinApi extends  BaseApi
{
	private $model;
    private $AppKey = '083a060dabcfd365b1fe5e2ffca8a6b2';     // key
    private $AppSecret = '246a7a099bc1';  // secret
	
	/**
	 * 测试地址
	 * */
	 public function _initialize()
    {
		$this->model = new Yunxinserver($this->AppKey,$this->AppSecret,'curl');
    }
	//http://smartgym.com/api/YunxinApi/test
	public function test(){
		var_dump($this->model->checkSumBuilder());
	}
	
	/**
     * 创建云信ID
     * http://smartgym.com/api/YunxinApi/createUserId/accid/zwzl_admin
	 * 
     */
    public function createUserId($accid)
    {
        if(empty($accid)||strlen($accid)<6){
			return template_error('accid已经长度不足6位,或者店铺id 不存在。');
        }
        $token = md5($accid);
        // 写入到云信服务器
        $info = $this->model->createUserIds($accid,'','{}','',$token);
		if($info['code']==200){
			return template_success();
		}else{
			return template_error($info['desc']);
		}
    }

    /**
	 * 获取指定用户的云信ID
	 * http://smartgym.com/api/YunxinApi/getUinfos/accid/zwzl_admin
	 */
    public function getUinfos($accid){
        $accid = array($accid);
        $info = $this->model->getUinfoss($accid);
		if($info['code']==200){
			return $this->api_ajax_success($info);
		}else{
			return $this->api_ajax_error("",$info['desc']);
		}
    }
	/**卓面端云信登录
	 * http://smartgym.com/api/YunxinApi/loginIn/accid/zwzl_admin
	 * */
	public function loginIn(){
		$accid=input("accid/s");
		db("GuestClient")->where("accid='$accid'")->setField(array("is_line"=>1));
		return $this->api_ajax_success();
	}
	/**卓面端云信登出
	 * http://smartgym.com/api/YunxinApi/loginOut/accid/loginOut
	 * */
	public function loginOut(){
		$accid=input("accid/s");
		db("GuestClient")->where("accid='$accid'")->setField(array("is_line"=>0));
		return $this->api_ajax_success();
	}
	/**
	 * 消息接受回掉
	 * http://smartgym.com/api/YunxinApi/receiveNotice/msg_no/15194378909385
	 * */
	public function receiveNotice(){
		$msg_no=input("msg_no/s");
		db("GuestMsg")->where("msg_no='$msg_no'")->setField(array("is_receive"=>1));
		return $this->api_ajax_success();
	}
    /**
	 * 发送自定义消息
	 * http://smartgym.com/api/YunxinApi/sendAttachMsg/accid/abc/type/1/brakemachine_no/01
	 * @param  $accid  接受人的 accid
	 * @param $type 类型 type=0 测试   type=1 正式
	 * @param $brackmachine_no 开门的闸机编号
	 */  
    public function sendAttachMsg($accid,$type=1,$brakemachine_no="01"){
    	if(empty($accid)){
        	$this->api_ajax_error("","accid 不能为空");
        }
		/**
		 * type=0 测试
		 * type=1 正式
		 * 
		 * flag=true 开门 
		 * flag=false 不开门
		 * 
		 * data=n   开n号门  
		 */
		$msg_no=time().rand(1000,9999);
		$json=json_encode(array("type"=>1,"msg"=>"","data"=>$brakemachine_no,"msg_no"=>$msg_no,"flag"=>($type==1?TRUE:FALSE)));
        $info = $this->model->sendAttachMsg('zwzl_admin',0,$accid,$json);
		if($info['code']==200){
			db("GuestMsg")->insert(array("guest_from"=>'zwzl_admin',"guest_to"=>$accid,"msg"=>$json,"back_record"=>json_encode($info,TRUE),"status"=>1,"msg_no"=>$msg_no));
			return $this->api_ajax_success("","成功");
		}else{
			db("GuestMsg")->insert(array("guest_from"=>'zwzl_admin',"guest_to"=>$accid,"msg"=>$json,"back_record"=>json_encode($info,TRUE),"status"=>0,"error_msg"=>$info['desc']));
			return $this->api_ajax_error("",$info['desc']);
		}
    }
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	/**
	 * 测试发送开门
	 * smartgym.com/api/YunxinApi/test_open_door
	 * */
	function test_open_door(){
		$url="http://".$_SERVER['HTTP_HOST']."/api/YunxinApi/OpenDoor";
		$time=time();
		$token=md5("0305201889");
		$result= post_json($url, array("token"=>$token,"time"=>$time,"brakemachine_no"=>"01","sign"=>$this->_get_sign($token, $time)));
		header('Content-Type:text/html; charset=utf-8');
		return $result;
	}
	/**
	 * 发送开门指令
	 * smartgym.com/api/YunxinApi/OpenDoor
	 * */
	function OpenDoor(){
		if(request()->isPost()){
			$token=input("token/s","");
			$time=input("time/d","");
			$brakemachine_no=input("brakemachine_no/s","");
			$sign=input("sign","");
			if(strlen($token)<5||strlen($time)<5||strlen($brakemachine_no)<1){
				return $this->api_ajax_error("","参数错误");
			}
			if(($time+300)<time()||($time-300)>time()){//接口调用时间操过5分钟
				return $this->api_ajax_error("","链接已经过期");
			}
			if($sign!=$this->_get_sign($token, $time)){
				return $this->api_ajax_error("","签名错误！");
			}
			$client=db("GuestClient")->where("token='$token' and brakemachine_no='$brakemachine_no' ")->find();
			if(empty($client)){
				return $this->api_ajax_error("","无效的token 或者闸机编号 ");
			}else{
				$this->sendAttachMsg($client['accid'],1,$client['brakemachine_no']);
			}	
		}else{
			return $this->api_ajax_error("","非法访问");
		}
	}
	/**
	 * 设备激活
	 * smartgym.com/api/YunxinApi/activation
	 */
	function activation(){
		$sn=input("sn/s");
		$activation_code=input("activation_code/s");
		$sign=input("sign/s");
		if(request()->isPost()){
			if(empty($sn)||empty($sign)){
				   return $this->ajax_error("","","参数错误");
			}else{
				if($sign!=md5($sn."ocm")){
				   return $this->ajax_error("","","签名错误");
				}else{
					if(in_array($activation_code, config("shape.shape_activation"))){
						if(in_array($sn, array_values(config("shape.shape_sn")))){
						}else{
							$shape_sn=config("shape.shape_sn");
							$shape_sn['activation_code']=$sn;
							sp_dynamic_config("shape",$shape_sn);
						}
						return $this->ajax_success("","","激活成功");
					}else{
						return $this->ajax_error("","","无效的激活码");
					}
				}
			}
		}else{
			return $this->ajax_error("","","非法访问");
		}
	} 
	//smartgym.com/api/YunxinApi/test1
	function test1(){
		var_dump(md5("abcdefg"."ocm"));die;
	}
	/**
	 * 获取签名
	 * @param $key
	 * @param $timestamp
	 * */
	private function _get_sign($key,$timestamp){
		return md5($key."_".$timestamp);
	}
}
